<?php

namespace App\Http\Controllers\OpenShop\Merchant;

use App\Http\Controllers\BaseController;
use App\Http\Validate\OpenShop\SiteLocation\SiteMissionValidate;
use App\Service\OpenShop\Merchant\SiteMissionService;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class SiteMissionController extends BaseController
{

    /**
     * 任务列表
     *
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function missionList(Request $request): JsonResponse
    {
        $shopId  = (int)$request->header('shopId');
        return $this->success(SiteMissionService::missionList($shopId));
    }


    /**
     * 确认选址信息
     *
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function confirmShopLocation(Request $request): JsonResponse
    {
        $shopId  = (int)$request->header('shopId');
        $missionId  = (int)$request->post('missionId', 0);
        SiteMissionService::confirmShopLocation($shopId, $missionId);
        return $this->success();
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws Exception|GuzzleException
     */
    public function rejectShopLocation(Request $request): JsonResponse
    {
        $requestParams = SiteMissionValidate::rejectConfirmSiteLocationValidate(
            $request->merge(["shopId" => (int)$request->header("shopId")])->all()
        );
        SiteMissionService::rejectShopLocation($requestParams->shopId, $requestParams->missionId, $requestParams->rejectReason);
        return $this->success();
    }

    /**
     * 选址详细信息
     *
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function shopLocationDetail(Request $request): JsonResponse
    {
        $shopId  = (int)$request->header('shopId');
        return $this->success(SiteMissionService::shopLocationDetail($shopId));
    }

}
